# t_pp_pr_lr_1990.R
# Outputs: Tables containing distributions of populist left and right parties in elections after 1990 presented in appendix
# Dependencies: master_elec.dta

setwd('')

library(haven)
library(tidyverse)
library(xtable)

df <- read_dta('master_elec.dta')

# Populist Participation Left, Right or Both
df$pp_lr <- 'None'
df$pp_lr[which(df$populistparty_leftonly == 1)] <- 'Populist Participation (Left)'
df$pp_lr[which(df$populistparty_rightonly == 1)] <- 'Populist Participation (Right)'
df$pp_lr[which(df$populistparty_both == 1)] <- 'Populist Participation (Both)'

# Populist Participation Left, Right or Both
df$pr_lr <- 'None'
df$pr_lr[which(df$l_populistpresence_leftonly == 1)] <- 'Populist Representation (Left)'
df$pr_lr[which(df$l_populistpresence_rightonly == 1)] <- 'Populist Representation (Right)'
df$pr_lr[which(df$l_populistpresence_both == 1)] <- 'Populist Representation (Both)'

df <- df %>% filter(election_year >= 1990)

# Full sample ------------------------------------------------------------------

df %>% group_by(pp_lr) %>%
    summarize(N = n()) %>%
    rename(` ` = pp_lr) %>%
    xtable() %>%
    print(include.rownames = F, booktabs = T, floating = F,
          file = 'tables/t_pp_lr_1990.tex')


df %>% group_by(pr_lr) %>%
    summarize(N = n()) %>%
    rename(` ` = pr_lr) %>%
    xtable() %>%
    print(include.rownames = F, booktabs = T, floating = F,
          file = 'tables/t_pr_lr_1990.tex')

# Eastern Europe ---------------------------------------------------------------

df_e <- df %>% filter(poco == 2)

df_e %>% group_by(pp_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pp_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pp_lr_east_1990.tex')

df_e %>% group_by(pr_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pr_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pr_lr_east_1990.tex')

# Western Europe ---------------------------------------------------------------

df_w <- df %>% filter(poco == 1)

df_w %>% group_by(pp_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pp_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pp_lr_west_1990.tex')

df_w %>% group_by(pr_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pr_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pr_lr_west_1990.tex')